package gui.newgui;

import gui.archerDbEditor.ArcherDbEditor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import tournamentData.TournamentData;
import core.GuiComponentLabels;

public class MouseListenerParticipantsTab implements MouseListener {

	@Override
	public void mouseClicked(MouseEvent arg0) {

		/** in case of doubleClick */
		if (arg0.getSource() instanceof JTable) {
			if (arg0.getClickCount() == 2) {
				JTable selectedTable = (JTable) arg0.getSource();
				int selectedID = Integer.parseInt(selectedTable.getValueAt(selectedTable.getSelectedRow(), 0).toString());
				TournamentData.getInstance().addRemovePlayer(selectedID);
			}

			/** in case of right-click */
			if (arg0.isMetaDown()) {
				final int selectedPlayerID = Integer.parseInt(((JTable) arg0.getSource()).getValueAt(((JTable) arg0.getSource()).getSelectedRow(), 0).toString());
				JPopupMenu popup;
				popup = new JPopupMenu();
				JMenuItem editMenuItem = new JMenuItem(GuiComponentLabels.EDIT_MENU);
				editMenuItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								ArcherDbEditor.getInstance().setLocationRelativeTo(null);
								ArcherDbEditor.getInstance().setVisible(true);
							}
						});
						try {
							ArcherDbEditor.getInstance().getController().buildTable();
							ArcherDbEditor.getInstance().getController().setPlayerSelected(selectedPlayerID);
							ArcherDbEditor.getInstance().getController().setDropDownMenus();
							ArcherDbEditor.getInstance().getController().setDropDownMenus();
							ArcherDbEditor.getInstance().getController().filloutForm();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});

				popup.add(editMenuItem);
				popup.show(arg0.getComponent(), arg0.getX(), arg0.getY());
			}
		}

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

}
